namespace CIS
{
    using CISLib;
    using System;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoCountPage : Page
    {
        protected DropDownList AreaDdl;
        protected DropDownList catDdl;
        protected DataGrid customerListDg;
        protected Image iCustomerInfo;
        protected Label lbArea;
        protected Label lbCat;
        protected Label lbLevel;
        protected Label lbProvince;
        protected DropDownList levelDdl;
        protected LinkButton lkbDown;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected Button SearchBtn;

        private void AddRow(DataTable myTable)
        {
            int count = 0;
            for (int i = 0; i < myTable.Rows.Count; i++)
            {
                count += (int) myTable.Rows[i][3];
            }
            object[] rowVals = new object[4];
            DataRowCollection rc = myTable.Rows;
            rowVals[0] = "-";
            rowVals[1] = "-";
            rowVals[3] = count;
            DataRow myNewRow = rc.Add(rowVals);
        }

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
        }

        private void customerListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.customerListDg.CurrentPageIndex = e.NewPageIndex;
            this.customerListDgDataBind();
        }

        private void customerListDgDataBind()
        {
            DataTable dt = ManageCustomerCount.GetCount(new Guid(this.AreaDdl.SelectedValue), new Guid(this.provinceDdl.SelectedValue), new Guid(this.catDdl.SelectedValue), Convert.ToInt32(this.levelDdl.SelectedValue)).Tables[0];
            this.AddRow(dt);
            this.customerListDg.DataSource = dt;
            this.customerListDg.DataBind();
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.lkbDown.Click += new EventHandler(this.lkbDown_Click);
            this.customerListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.customerListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.customerListDg.PageSize = 50;
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            this.catDdl.DataSource = ManageCAT.GetCAT(new Guid(this.AreaDdl.SelectedValue));
            this.catDdl.DataTextField = "CATGroupName";
            this.catDdl.DataValueField = "CATGroupId";
            this.catDdl.DataBind();
            this.levelDdl.Items.Clear();
            ListItem tmpItemAll = new ListItem();
            tmpItemAll.Text = "All";
            tmpItemAll.Value = "0";
            this.levelDdl.Items.Add(tmpItemAll);
            for (int i = 1; i <= 5; i++)
            {
                string tmpStr = "Level " + i.ToString();
                ListItem tmpItem = new ListItem();
                tmpItem.Text = tmpStr;
                tmpItem.Value = i.ToString();
                this.levelDdl.Items.Add(tmpItem);
            }
        }

        private void lkbDown_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = ManageCustomerCount.GetCount(new Guid(this.AreaDdl.SelectedValue), new Guid(this.provinceDdl.SelectedValue), new Guid(this.catDdl.SelectedValue), Convert.ToInt32(this.levelDdl.SelectedValue)).Tables[0];
            this.AddRow(dt);
            StringWriter sw = new StringWriter();
            for (int ii = 0; ii < dt.Columns.Count; ii++)
            {
                if (ii == (dt.Columns.Count - 1))
                {
                    sw.Write(dt.Columns[ii].ColumnName + "\n");
                }
                else
                {
                    sw.Write(dt.Columns[ii].ColumnName + ",");
                }
            }
            for (int ii = 0; ii < dt.Rows.Count; ii++)
            {
                DataRow dr = dt.Rows[ii];
                for (int jj = 0; jj < dt.Columns.Count; jj++)
                {
                    if (jj == (dt.Columns.Count - 1))
                    {
                        sw.Write(dr[dt.Columns[jj].ColumnName] + "\n");
                    }
                    else
                    {
                        sw.Write(dr[dt.Columns[jj].ColumnName] + ",");
                    }
                }
            }
            sw.Close();
            base.Response.AddHeader("Content-Disposition", "attachment; filename=CustomerCount" + DateTime.Now.Millisecond.ToString() + ".csv");
            base.Response.ContentType = "application/ms-excel";
            base.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            base.Response.Write(sw);
            base.Response.End();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.SetFromImage();
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.customerListDgDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "lbArea", language, path);
            this.lbCat.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "lbCat", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "lbProvinceOverSea", language, path);
                this.customerListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "regionNameOverSea", language, path);
            }
            else
            {
                this.lbProvince.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "lbProvince", language, path);
                this.customerListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "regionName", language, path);
            }
            this.SearchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "SearchBtn", language, path);
            this.lbLevel.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "lbLevel", language, path);
            this.customerListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "catName", language, path);
            this.customerListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "level", language, path);
            this.customerListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoCountPage", "count", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerStat.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "images/title_CustomerStatEn.jpg";
                    break;
            }
        }
    }
}

